package com.atguigu.atcrowdfunding.controller;

//import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TMenu;
//import com.atguigu.atcrowdfunding.service.TMenuService;
import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.service.TMenuService;
import com.atguigu.atcrowdfunding.util.Const;
import com.github.pagehelper.PageInfo;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DispatcherController {
    Logger log= LoggerFactory.getLogger(DispatcherController.class);
    @Autowired
    TMenuService menuService;

    @Autowired
      TAdminService adminService;

    @RequestMapping("/index")   //http://localhost:8080/atcrowdfunding_single_Web_exploded/
    public String index(){
       log.debug("跳转到系统主页面");
        return "index";
    }
    @RequestMapping("/toLogin")
    public String login(){
        log.debug("跳转到登录主页面");
        return "login";
    }

    @RequestMapping("/reg")
    public String reg(){
        log.debug("跳转到注册主页面");
        return "reg";
    }

    @RequestMapping("/main")
    public String main(HttpSession session){
        log.debug("跳转到后台系统main页面");
        if(session==null){
            return "redirect:/toLogin";
        }
        //存放父菜单
       List<TMenu> menuList = (List<TMenu>)session.getAttribute("meuList");//为什么是session里获得
        log.debug("meuList={}",menuList);
        if(menuList==null) {
            log.debug("muuList=null ====>>>查询侧边栏 菜单树");
            menuList = menuService.listMenuAll();
            session.setAttribute("meulist", menuList);
        }
        return "main";
    }


//    @RequestMapping("/Logout")
//    public String logout(HttpSession session){
//        log.debug("注销系统");
//        if(session!=null){
//            session.removeAttribute(Const.LOGIN_ADMIN);
//            session.invalidate();
//        }
//        //重定向到主页面
//        return "redirect:/index";
//        //return "/index";
//    }

//    @RequestMapping("/doLogin")
//    public String doLogin(String loginacct, String userpswd, HttpSession session, Model model){
//        log.debug("开始登录");
//        log.debug("loginacct={}",loginacct);
//        log.debug("userpassword={}",userpswd);
//        Map<String,Object> paramMap=new HashMap<String,Object>();
//        paramMap.put("loginacct",loginacct);
//        paramMap.put("userpswd",userpswd);
//        try {
//            TAdmin admin=adminService.getTAdminByLogin(paramMap);
//            log.debug("登录成功");
//            session.setAttribute(Const.LOGIN_ADMIN,admin);
//            return "redirect:/main";
//           // return "main"; //避免表单重复提交，采用重定向
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.debug("登录失败={}",e.getMessage());
//            model.addAttribute("message",e.getMessage());
//            return "login";
//        }

//    }
}
